package com.uwo.house.util;


import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by PHS on 2017/7/20.
 */
public class UtilDownload {

    public static boolean getImage(String imgPath,String destPath) {
        try {
            URL url = new URL(imgPath);
            URLConnection conn = url.openConnection();
            InputStream inputStream = conn.getInputStream();

            File file = new File(destPath);
            FileOutputStream fout = new FileOutputStream(file);

            int i = 0;
            while ((i = inputStream.read()) != -1) {
                fout.write(i);
            }
            fout.close();
            inputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

}
